<?php

namespace app\admin\controller;

use app\admin\model\SstalinkData;
use app\admin\model\SstalinkSensor;
use app\common\controller\Backend;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Data extends Backend
{

    /**
     * SstalinkData模型对象
     */
    protected $noNeedLogin=['number','text','gps','image','boolean'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('SstalinkData');
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function number($id,$device_id)
    {
        $admin = Session::get("admin");
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = Db::table("sstalink_data_number")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->count();

            $list = Db::table("sstalink_data_number")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $showtype = model('SstalinkSensor')
//            ->where("id", "in", function ($query) use ($id, $admin) {
//                $query->table("sstalink_sensor")
//                    ->where("deviceid", "in", function ($query) use ($admin) {
//                        $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                    })
//                    ->field("id");
//            })
            ->where("deviceid","=",$device_id)
            ->where("id","=",$id)
            ->column("showtype");
        $this->view->assign("showtype", $showtype[0]);
        return $this->view->fetch();
    }

    public function text($id,$device_id)
    {
        $admin = Session::get("admin");
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = Db::table("sstalink_data_text")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->count();

            $list = Db::table("sstalink_data_text")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function gps($id,$device_id)
    {
        $admin = Session::get("admin");
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = Db::table("sstalink_data_point")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->count();

            $list = Db::table("sstalink_data_point")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function image($id,$device_id)
    {
        $admin = Session::get("admin");
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = Db::table("sstalink_data_base64")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->count();

            $list = Db::table("sstalink_data_base64")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function boolean($id,$device_id)
    {
        $admin = Session::get("admin");
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = Db::table("sstalink_data_boolean")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->count();

            $list = Db::table("sstalink_data_boolean")
                ->where($where)
                ->where("sensorid", "in", function ($query) use ($id, $admin) {
                    $query->table("sstalink_sensor")
//                        ->where("deviceid", "in", function ($query) use ($admin) {
//                            $query->table("sstalink_device")->where("ownerid", "=", $admin->id)->field("deviceid");
//                        })
                        ->where("id","=",$id)
                        ->field("id");
                })
                ->where("deviceid","=",$device_id)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }


}
